package com.sanpu.kxe.module.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseDialog;
import com.sanpu.kxe.business.main.bean.UserSignOutput;
import com.sanpu.kxe.business.main.model.MainModel;

import butterknife.ButterKnife;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：签到
 * Created by 薛勇军 on 2018/8/6.
 */

public class UserSignDialog extends BaseDialog<UserSignDialog> {

    private LinearLayout vContentContainer;
    private ProgressBar vProgressBar;
    private CharSequence content;
    private TextView vContent;
    private TextView vTitle;
    private String title;
    private CompositeSubscription mSubscription;

    public UserSignDialog content(CharSequence content) {
        this.content = content;
        return this;
    }

    public UserSignDialog(Context context, @NonNull CompositeSubscription mSubscription) {
        super(context);
        this.mSubscription = mSubscription;
    }

    @Override
    public View onCreateView() {
        showAnim(new BaseAnimatorSet() {
            @Override
            public void setAnimation(View view) {
            }
        });

        View inflate = View.inflate(getContext(), R.layout.dialog_user_sign, null);
        vContent = ButterKnife.findById(inflate, R.id.content);
        vTitle = ButterKnife.findById(inflate, R.id.title);

        vContentContainer = ButterKnife.findById(inflate, R.id.content_container);
        vProgressBar = ButterKnife.findById(inflate, R.id.progressBar);
        inflate.setPadding(dp2px(15), dp2px(15), dp2px(15), dp2px(15));

        if (!TextUtils.isEmpty(title)) {
            vTitle.setVisibility(View.VISIBLE);
            vTitle.setText(title);
        }
        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        vContent.setText(content);
        vProgressBar.setVisibility(View.VISIBLE);
        Subscription subscription = MainModel.getInstance().userSign()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserSignOutput>() {
                    @Override
                    public void onCompleted() {
                        vProgressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError(Throwable e) {
                        vProgressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onNext(UserSignOutput userSignOutput) {
                        vContentContainer.setVisibility(View.VISIBLE);
                        if (userSignOutput != null) {
                            if (TextUtils.isEmpty(userSignOutput.getBp())) {
                                vTitle.setText("今日您已签到");
                                vContent.setText("已连续签到" + userSignOutput.getDay() + "天");
                            } else {
                                vTitle.setText("签到成功");
                                vContent.setText("恭喜您获得个" + userSignOutput.getBp() +
                                        "积分\n已连续签到" + userSignOutput.getDay() + "天");
                            }
                        } else {
                            vTitle.setText("今日已签到");
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    public UserSignDialog title(String title) {
        this.title = title;
        return this;
    }
}
